// __PUBLIC__/js/page/cms/template/tplmanage
require(['../../common'], function() {
  require(['template.tplmanage'])
})

define('template.tplmanage', [
  'jquery'
], function($) {
  'use strict';


  function createOptions(data) {
    var html = '<option value="0">-- 请选择 --</option>'
    $.each(data, function(key, val) {
      html += '<option value="' + val.id + '">' + val.name + '</option>'
    })
    return html
  }

  /**
   * 获取分类列表
   * @param  {number} sid site id
   * @param  {number} pid parent category id
   * @return {}
   */

  function getCates() {
    // $.get(APP + '?app=cms&m=Cate&a=queryCates', {
    //   sid: sid,
    //   pid: pid
    // })
    //   .done(function(data) {
    //     console.log(data)
    //     // $tbody.html(data)
    //   })/
    var url = APP + '?app=cms&m=Template&a=tplmanage&sid=' + sid
    location.href = url
  }

  function addCate() {
    var url = APP + '?app=cms&m=Cate&a=addcate&sid=' + sid +
      (pid ? '&pid=' + pid : '')
    location.href = url
  }

  var $sites = $('#sites')
  var $btnAdd = $('#J_add')
  var $sid = $('#sid')
  var sid = $sid.val()


  $.getJSON(APP + '?app=cms&m=Cate&a=siteTreeList')
    .done(function(data) {
      // console.log(data)
      $sites.html(createOptions(data)).val(sid)
    })

  $sites.on('change', function() {
    var val = this.value
    if (val) {
      sid = val
      getCates()
    }
  })

  $btnAdd.on('click', function() {
    if (!sid) {
      alert('请选择站点')
    } else {
      addCate()
    }
  })

})
